package com.ellfors.common.ext

import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.template.IProvider
import com.alibaba.android.arouter.launcher.ARouter

fun Any.injectRouter() {
    ARouter.getInstance()
        .inject(this)
}

fun navigation(path: String, build: Postcard.() -> Unit = {}) {
    val postcard =
        ARouter.getInstance()
            .build(path)
    postcard.build()
    postcard.navigation()
}

@Suppress("UNCHECKED_CAST")
fun <T : IProvider> findProvider(path: String): T {
    return ARouter.getInstance()
        .build(path)
        .navigation() as T
}

private fun <T : IProvider> lazyProvider(path: String) = lazy {
    findProvider(path) as T
}